// pages/Projects/ReceiveAward/ReceiveAward.js
//获取应用实例
const app = getApp()
const assetsAddress = app.globalData.global.assetsIp + '/' + app.globalData.global.assetsName;
import { shareAwardDetail , confirmReceiveAward } from '../../../api/reward';
import { getBankCard } from '../../../api/card';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    awardId: '',
    award: {
      awardAmount: 0, // 奖励金额
      amount: 0, // 实发金额
      realName: '', // 姓名
      idCard: '', // 身份证号
      serviceRate: 0, // 代扣代缴个税比例
      state: '0',
    },
    bankCardId: '',
    cardList: [],
    assetsAddress: assetsAddress,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    if(options.awardId){
      this.setData({
        awardId: options.awardId,
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if(this.data.awardId != ''){
      this.getDeatil();
    }
    this.getCard();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // 获取佣金详情信息
  getDeatil(){
    let _this = this;
    shareAwardDetail(_this.data.awardId)
    .then(res => {
      if(res.success){
        _this.setData({
          award: res.data,
        })
      }else{
        wx.showToast({
          title: res.errMessage,
          icon: 'none',
        })
      }
    })
  },
  addBankCard(){
    wx.navigateTo({
      url:"/pages/Tabbar/My/bankCard/addBankCard",
    });
  },
  // 获取银行卡列表
  getCard(){
    let _this = this;
    getBankCard({ page: 1,})
    .then(res => {
      if(res.success){
        _this.setData({
          cardList: res.data,
        })
        if(res.data.length > 0){
          _this.setData({
            bankCardId: res.data[0].id
          })
        }
      }else{
        wx.showToast({
          title: res.errMessage,
          icon: 'none',
        })
      }
    })
  },
  // 选择银行卡
  selCard(e){
    let item = e.target.dataset.item||e.currentTarget.dataset.item;
    this.setData({
      bankCardId: item.id,
    })
  },
  // 确认领取
  submit(){
    let _this = this;
    if(_this.data.bankCardId == ''){
      wx.showToast({
        title: '请选择收款账户',
        icon: 'none',
      })
      return;
    }
    confirmReceiveAward(_this.data.awardId,_this.data.bankCardId)
    .then(res => {
      if(res.success){
        wx.navigateTo({
          url: '/pages/link/RewardSuccess/RewardSuccess?awardId='+ this.data.awardId,
        })
      }else{
        wx.showToast({
          title: res.errMessage,
          icon: 'none',
        })
      }
    })
  },
})